package com.cd.javacxy.module.web.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cd.javacxy.common.model.entity.CxyLike;
import com.cd.javacxy.common.model.enums.ArticleType;
import com.cd.javacxy.common.model.enums.TextLengthEnum;
import com.cd.javacxy.common.model.query.LikeQuery;
import com.cd.javacxy.common.model.query.UpdateQuery4ArticleCount;
import com.cd.javacxy.exception.BussinessException;
import com.cd.javacxy.module.web.dao.*;
import com.cd.javacxy.module.web.service.CxyLikeService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * CxyLike的服务接口的实现类
 *
 * @author yanghy
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CxyLikeServiceImpl implements CxyLikeService {

    private final CxyLikeDao cxyLikeDao;
    private final CxyTopicDao cxyTopicDao;
    private final CxyKnowledgeDao cxyKnowledgeDao;
    private final CxyAskDao cxyAskDao;
    private final CxyBlogDao cxyBlogDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = BussinessException.class)
    public int addLike(CxyLike like) throws BussinessException {

        if (like.getArticleId() == null || like.getArticleType() == null
                || StrUtil.isEmpty(like.getTitle()) ||
                like.getTitle().length() > TextLengthEnum.TEXT_300_LENGTH.getLength()) {
            throw new BussinessException("参数错误");
        }

        LikeQuery likeQuery = new LikeQuery(like.getArticleId(),
                like.getArticleType(), like.getUserId());
        CxyLike l = this.findLikeByKey(likeQuery);
        if (l != null) {
            throw new BussinessException("您已经赞过了");
        }
        like.setCreateTime(LocalDateTime.now());
        this.cxyLikeDao.insert(like);

        UpdateQuery4ArticleCount updateQuery4ArticleCount = new UpdateQuery4ArticleCount();
        updateQuery4ArticleCount.setAddLikeCount(Boolean.TRUE);
        updateQuery4ArticleCount.setArticleId(like.getArticleId());
        if (like.getArticleType() == ArticleType.TOPIC) {
            this.cxyTopicDao.updateInfoCount(updateQuery4ArticleCount);
        } else if (like.getArticleType() == ArticleType.KNOWLEDGE) {
            this.cxyKnowledgeDao.updateInfoCount(updateQuery4ArticleCount);
        } else if (like.getArticleType() == ArticleType.Ask) {
            this.cxyAskDao.updateInfoCount(updateQuery4ArticleCount);
        } else if (like.getArticleType() == ArticleType.BLOG) {
            this.cxyBlogDao.updateInfoCount(updateQuery4ArticleCount);
        } else {
            throw new BussinessException("参数错误");
        }
        return 0;
    }

    public CxyLike findLikeByKey(LikeQuery likeQuery) {
        List<CxyLike> likes = this.cxyLikeDao.selectList(likeQuery);
        if (likes.isEmpty()) {
            return null;
        }
        return likes.get(0);
    }
}